package com.demo.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class Pre2Filter extends ZuulFilter {
    @Override
    public String filterType() {  //过滤器类型
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {   //过滤器执行顺序
        return 1;
    }

    @Override
    public boolean shouldFilter() {  //过滤器是否起作用，true为是
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest httpServletRequest = requestContext.getRequest();
        System.out.println("token="+httpServletRequest.getHeader("token"));
        if(httpServletRequest.getParameter("token") !=null){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public Object run() throws ZuulException {  //过滤器逻辑处理
        System.out.println("Pre2Filter");
        return null;
    }
}
